package com.hexacta.hrs.web.struts.actions.reports;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.hexacta.hrs.domain.mentoring.MeetingStatus;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.web.struts.actions.BaseReportAction;

/**
 * Action que se encarga de filtrar los resultados para generar el reporte
 * Meetings Report.
 * 
 * @author ncortinez
 * 
 */
public class MeetingsReportAction extends BaseReportAction {

    private static final long serialVersionUID = -40110813062065898L;

    private EmployeeService employeeService;

    private static String YES = "Yes";

    private static String NO = "No";

    private String selectedMentorId;

    private String selectedMentorName;

    private String selectedEmployeeId;

    private String selectedEmployeeName;

    private Date meetingDateFrom;

    private Date meetingDateTo;

    private String checkerHRName;

    private String selectedStatus;

    private Boolean delayed;

    private Boolean onlyCurrent;

    @Override
    protected Map<String, Object> getReportFilters() {
        Map<String, Object> filters = new HashMap<String, Object>();

        // Filtro por MeetingStatus
        if (!StringUtils.isEmpty(selectedStatus)) {
            try {
                filters.put("status", MeetingStatus.valueOf(selectedStatus));
            } catch (Exception e) {
            }
        }

        // Filtro por Employee
        if (!StringUtils.isEmpty(selectedEmployeeName)) {
            long id = NumberUtils.toLong(selectedEmployeeId, Long.MIN_VALUE);
            if (Long.MIN_VALUE != id) {
                filters.put("employee", getEmployeeService().searchById(id));
            } else {
            }
        }

        // Filtro por Mentor
        if (!StringUtils.isEmpty(selectedMentorName)) {
            long id = NumberUtils.toLong(selectedMentorId, Long.MIN_VALUE);
            if (Long.MIN_VALUE != id) {
                filters.put("mentor", getEmployeeService().searchById(id));
            } else {
            }
        }

        // Filtro por fecha desde
        if (meetingDateFrom != null) {
            filters.put("dateFrom", meetingDateFrom);
        }

        // Filtro por fecha hasta
        if (meetingDateTo != null) {
            filters.put("dateTo", meetingDateTo);
        }

        // Filtro por checker
        if (!StringUtils.isEmpty(checkerHRName)) {
            filters.put("checkedByHR", (checkerHRName.equals(MeetingsReportAction.YES) ? Boolean.TRUE : Boolean.FALSE));
        }

        // Filtro por delayed
        if (getDelayed() != null && getDelayed().equals(Boolean.TRUE)) {
            filters.put("onlyDelayed", getDelayed());
        }

        // Filtro por onlyCurrentRelationships
        filters.put("onlyCurrentRelationships", getOnlyCurrent());

        return filters;
    }

    public List<String> getCheckerByHROptions() {
        List<String> checkerOptions = new ArrayList<String>();
        checkerOptions.add(MeetingsReportAction.YES);
        checkerOptions.add(MeetingsReportAction.NO);

        return checkerOptions;
    }

    public List<MeetingStatus> getAllStatuses() {
        return MeetingStatus.getAllMeetingStatus();
    }

    public String getSelectedMentorId() {
        return selectedMentorId;
    }

    public void setSelectedMentorId(String selectedMentorId) {
        this.selectedMentorId = selectedMentorId;
    }

    public String getSelectedMentorName() {
        return selectedMentorName;
    }

    public void setSelectedMentorName(String selectedMentorName) {
        this.selectedMentorName = selectedMentorName;
    }

    public String getSelectedEmployeeId() {
        return selectedEmployeeId;
    }

    public void setSelectedEmployeeId(String selectedEmployeeId) {
        this.selectedEmployeeId = selectedEmployeeId;
    }

    public String getSelectedEmployeeName() {
        return selectedEmployeeName;
    }

    public void setSelectedEmployeeName(String selectedEmployeeName) {
        this.selectedEmployeeName = selectedEmployeeName;
    }

    public Date getMeetingDateFrom() {
        return meetingDateFrom;
    }

    public void setMeetingDateFrom(Date meetingDateFrom) {
        this.meetingDateFrom = meetingDateFrom;
    }

    public Date getMeetingDateTo() {
        return meetingDateTo;
    }

    public void setMeetingDateTo(Date meetingDateTo) {
        this.meetingDateTo = meetingDateTo;
    }

    public void setSelectedStatus(String selectedStatus) {
        this.selectedStatus = selectedStatus;
    }

    public String getSelectedStatus() {
        return selectedStatus;
    }

    public String getCheckerHRName() {
        return checkerHRName;
    }

    public void setCheckerHRName(String checkerHRName) {
        this.checkerHRName = checkerHRName;
    }

    public void setDelayed(Boolean delayed) {
        this.delayed = delayed;
    }

    public Boolean getDelayed() {
        return delayed;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setOnlyCurrent(Boolean onlyCurrent) {
        this.onlyCurrent = onlyCurrent;
    }

    public Boolean getOnlyCurrent() {
        return (onlyCurrent != null) ? onlyCurrent : Boolean.TRUE;
    }

}
